<template>
  <div class="container-login">
    <div class="pannel_box">
      <div class="img_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        ref="formRef"
        class="form_but"
        :model="loginForm"
        :rules="formRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            :prefix-icon="icon1"
            @blur="onblur"
            @focus="onfocus"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            :prefix-icon="icon2"
            type="password"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-button type="primary" round @click="loginGo">登录</el-button>
        <el-button type="info" round @click="resetBut">重置</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      formRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
          {
            min: 3,
            max: 18,
            message: '长度在 3 到 18 个字',
            trigger: 'blur',
          },
        ],
        password: [
          {
            min: 6,
            max: 18,
            message: '长度在 6 到 18 个字符',
            trigger: 'blur',
          },
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
      },
      icon1: 'el-icon-user',
      icon2: 'el-icon-lock',
    }
  },

  methods: {
    onfocus() {
      this.icon1 = 'el-icon-user-solid'
    },
    onblur() {
      this.icon1 = 'el-icon-user'
    },
    resetBut() {
      this.$refs.formRef.resetFields()
    },
    loginGo() {
      this.$refs.formRef.validate(async (vaild) => {
        if (!vaild) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        localStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.container-login {
  background-color: rgba(11, 129, 175, 0.932);
  height: 100%;
  text-align: center;
  .pannel_box {
    width: 500px;
    height: 350px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    .img_box {
      width: 150px;
      height: 150px;
      border: 3px solid springgreen;
      background-color: #fff;
      border-radius: 50%;
      overflow: hidden;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        background-color: #fff;
        width: 100%;
        height: 100%;
      }
    }
  }
}
.form_but {
  width: 100%;
  position: absolute;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
  padding: 0 100px;
}
</style>